let http=require("http");
let querystring=require("querystring");

module.exports=function(path,obj){
  let promise=new Promise(function (resolve,reject) {
    const postData = querystring.stringify(obj);//将参数进行序列化

const options = {
  hostname: 'localhost',
  port: 9999,
  path: path,
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': Buffer.byteLength(postData)
  }
};

const req = http.request(options, (res) => {
  if(res.statusCode!=200){
    reject(res.statusCode);
  }
  // console.log(`状态码: ${res.statusCode}`);
  // console.log(`响应头: ${JSON.stringify(res.headers)}`);
  res.setEncoding('utf8');
  var rawData=" ";
  res.on('data', (chunk) => {
    // console.log(`响应主体: ${chunk}`);
    rawData+=chunk;
  });
  res.on('end', () => {
    // console.log('响应中已无数据。');
    resolve(rawData);
  });
});

req.on('error', (e) => {
  reject(`请求遇到问题: ${e.message}`);
});

// 写入数据到请求主体
req.write(postData);
req.end();
  });
  return promise;
}
